<?php
namespace app\admin\controller;

use think\console\Command;
use think\console\Input;
use think\console\Output;

use think\Pgettext\Mo;
use think\Pgettext\Po;
use think\Pgettext\Stringset;

class mopoTranslate extends Command
{
    protected function configure()
    {
        $this->addArgument('url');            //处理文件路径
        $this->addArgument('language');       //指定语言，翻译其他语言
        $this->setName('mopo:translate')
            ->setDescription('translate mo file');
    }

    //获取连接下的所有mo po文件
    public function folder_list($dir){
        $dir .= substr($dir, -1) == '/' ? '' : '/';
        $dirInfo = array();
        foreach (glob($dir.'*') as $v) {
            if(substr($v,strlen($v)-3,strlen($v))==".mo"){
                $list=explode('/',$v);
                $language="";
                foreach($list as $k => $i){
                    if($i=="LC_MESSAGES"){
                        $language=$list[$k-1];
                    }
                }
                if(!empty($language)){
                    $dirInfo['mo'][$language][] = $v;
                }
            }
            if(substr($v,strlen($v)-3,strlen($v))==".po"){
                $list=explode('/',$v);
                $language="";
                foreach($list as $k => $i){
                    if($i=="LC_MESSAGES"){
                        $language=$list[$k-1];
                    }
                }
                if(!empty($language)){
                    $dirInfo['po'][$language][] = $v;
                }
            }
            if(is_dir($v)){
                $dirInfo = array_merge($dirInfo, $this->folder_list($v));
            }
        }
        return $dirInfo;
    }

    public function translateFile($stringset,$language){
        foreach($stringset->catalog() as $key => $val){
            if(!empty($val['id'])){
                $msgid=addslashes(addslashes($val['id']));
                $info = db('msgstr')->alias('a')->field('msgid,msgstr')->where("msgid='{$msgid}' and language='{$language}' and flag=1")->join('msgid b','a.msgid_id = b.id')->find();
                if(!empty($info)){
                    $stringset->set($val['id'],stripslashes($info['msgstr']));
                }else{
                    $stringset->set($val['id'],"");
                }
            }
        }
        return $stringset;
    }

    protected function execute(Input $input, Output $output)
    {
        $args = $input->getArguments();
        $mo = new Mo();
        $po = new Po();
        $language_arr=array("ug_CN","kk_CN","zh_CN","en_US");
        $set_language=$args['language'];
        if(empty($args['url'])) {
            //$dir = ROOT_PATH . "uploads/mopo/";
            print_r("please import need to translate file");exit;
        }else{
            $dir =$args['url'];
        }
        $file=$this->folder_list($dir);
        if(empty($file)){
            print_r($dir." not find mopo file");exit;
        }
       // print_r($language_arr);exit;

        $mosum=0;$posum=0;
        foreach($file['mo'] as $key => $fval) {
            foreach($fval as $k => $f){
                $mosum++;
                $fil_name=basename($f);
                $stringset=$mo->fromFile($f);
                print_r($f);print_r("\n");
                $re_stringset=$this->translateFile($stringset,$key);
                file_put_contents($f,$mo->toString($re_stringset));

                //翻译其他语种
                if(!empty($set_language)){
                    foreach($stringset->catalog() as $kk => $vv){
                        $stringset->set($vv['id'],"");
                    }
                    //print_r($stringset->catalog());exit;
                    foreach($language_arr as $l){
                        if($l!=$key){
                            $re_s=$this->translateFile($stringset,$l);//翻译
                            $re_s->set("","Language: {$l}\nContent-Type: text/plain; charset=UTF-8");
                            //print_r($head);exit;
                            $f_new=str_replace($key,$l,$f);
                            $file_url=dirname($f_new);
                            if (!file_exists($file_url)) { // 如果不存在则创建
                                mkdir($file_url,0777,true);
                                //print_r($file_url);
                            }
                            file_put_contents($f_new,$mo->toString($re_s));
                        }
                    }
                }
            }

        }
        foreach($file['po'] as $key => $fval) {
            foreach($fval as $k => $f){
                $posum++;
                $fil_name=basename($f);
                $stringset=$po->fromFile($f);
                print_r($f);print_r("\n");
                $re_stringset=$this->translateFile($stringset,$key);
                file_put_contents($f,$po->toString($re_stringset));

                //翻译其他语种
                if(!empty($set_language)){
                    foreach($stringset->catalog() as $kk => $vv){
                        $stringset->set($vv['id'],"");
                    }
                    foreach($language_arr as $l){
                        if($l!=$key){
                            $re_s=$this->translateFile($stringset,$l);
                            $re_s->set("","Language: {$l}\nContent-Type: text/plain; charset=UTF-8");
                            $f_new=str_replace($key,$l,$f);
                            $file_url=dirname($f_new);
                            if (!file_exists($file_url)) { // 如果不存在则创建
                                mkdir($file_url,0777,true);
                                //print_r($file_url);
                            }
                            file_put_contents($f_new,$po->toString($re_s));
                        }
                    }
                }
            }
        }
        if($mosum){
            print_r('Translate '.$mosum.' mo files');print_r("\n");
        }
        if($posum){
            print_r('Translate '.$posum.' po files');print_r("\n");
        }
        print_r('ok');
    }
}